// 订单状态 1待审核 2审核驳回 3审核通过 4待确认 5取消 6待收货 7待拣货 8待出库 9部分出库
// 10全部出库 11部分收货 12已完成
import { hasIn } from 'lodash'
import { getCertificate } from '@const/productCenterApiV2'
export const STATUS_LABEL = {
  TO_AUDIT: '审核中',
  REJECTED: '审核驳回',
  AUDITED: '审核通过',
  TO_CONFIRM: '待确认',
  CANCELED: '已取消',
  TO_RECEIVE: '待收货',
  TO_PICKUP: '待拣货',
  TO_SHIPMENT: '待出库',
  PART_SHIPMENT: '部分出库',
  ON_SHIPMENT: '全部出库',
  PART_RECEIVED: '部分收货',
  FINISHED: '已完成',
  RETURN: '待术后还回'
}
export const ORDER_STATUS = {
  1: STATUS_LABEL.TO_AUDIT,
  2: STATUS_LABEL.REJECTED,
  3: STATUS_LABEL.AUDITED,
  4: STATUS_LABEL.TO_CONFIRM,
  5: STATUS_LABEL.CANCELED,
  6: STATUS_LABEL.TO_RECEIVE,
  7: STATUS_LABEL.TO_PICKUP,
  8: STATUS_LABEL.TO_SHIPMENT,
  9: STATUS_LABEL.PART_SHIPMENT,
  10: STATUS_LABEL.ON_SHIPMENT,
  11: STATUS_LABEL.PART_RECEIVED,
  12: STATUS_LABEL.FINISHED,
  13: STATUS_LABEL.RETURN
}

// 操作类型
export const OPERATE_TEXT = {
  CREATE: '创建',
  AUDIT_REJECTED: '审核驳回',
  EDIT_REJECTED: '驳回编辑',
  AUDITED: '审核通过',
  ORDER_CONFIRMED: '确认接单',
  ORDER_REJECTED: '拒绝接单',
  SURGERY_RETURNED: '术后退回确认'
}
export const OPERATE_TYPE = {
  1: 'CREATE',
  2: 'AUDIT_REJECTED',
  3: 'EDIT_REJECTED',
  4: 'AUDITED',
  5: 'ORDER_CONFIRMED',
  6: 'ORDER_REJECTED',
  7: 'SURGERY_RETURNED'
}

// 单位选项 todo 待完善，缺少较多，按prd补充 => 暂时修改为不可修改
export const UNIT_OPTIONS = [
  {
    value: 1,
    label: '根'
  },
  {
    value: 2,
    label: '盆'
  },
  {
    value: 3,
    label: '箱'
  }
]

// 注册地
export const PRODUCT_REGISTER_TYPE = {
  1: '国产',
  2: '港澳台',
  3: '进口'
}

// 产品大类
export const PRODUCT_CATEGORY = {
  1: '高值',
  2: '低值',
  3: '试剂',
  4: '设备',
  5: '低值'
}

// 管理分类
export const PRODUCT_MANAGE_CATEGORY = {
  1: '一类',
  2: '二类',
  3: '三类'
}

// 操作记录表头
export const commonOperateRecordColumns = [
  {
    width: 60,
    type: 'index',
    label: '序号'
  },
  {
    minWidth: 120,
    prop: 'operationType',
    label: '操作类型',
    formatter: row => {
      if (!row.operationType && row.operationType !== 0) {
        return ''
      } else {
        return OPERATE_TEXT[OPERATE_TYPE[row.operationType]]
      }
    }
  },
  {
    minWidth: 120,
    prop: 'operator',
    label: '操作员'
  },
  {
    width: 180,
    prop: 'createdAt',
    label: '操作日期'
  },
  {
    minWidth: 120,
    prop: 'remark',
    label: '备注'
  }
]

export const commonSurgicalListColumns = [
  { type: 'selection', width: '50' },
  { minWidth: '150', prop: 'platCode', label: '平台编码' },
  { minWidth: '150', prop: 'prodNameCn', label: '产品名称' },
  { width: '180', prop: 'companyProdCode', label: '产品编码' },
  {
    minWidth: '150',
    prop: 'specification',
    label: '规格型号',
    formatter: row => (row.spec || ''),
    'show-overflow-tooltip': true
  },
  { minWidth: '180', prop: 'manufacturer', label: '生产厂家', showTooltipWhenOverflow: true },
  { minWidth: '250', prop: 'registNo', label: '注册证号（或备案号）', showTooltipWhenOverflow: true },
  {
    width: '120',
    prop: 'price',
    label: '单价(￥)',
    formatter: row => formatPrice(row.price)
  },
  // {width: '180', prop: 'purchaseNum', label: '采购数量'},
  { width: '120', prop: 'unit', label: '最小包装单位' },
  {
    minWidth: '100',
    prop: 'manageType',
    label: '管理分类',
    formatter: row => PRODUCT_MANAGE_CATEGORY[row.manageType]
  },
  // {
  //   width: '120',
  //   prop: 'registType', // 接口mock这个字段是错的，还有一个registerArea，非常混乱
  //   label: '注册地',
  //   formatter: row => PRODUCT_REGISTER_TYPE[row.registType]
  // },
  {
    width: '100',
    prop: 'bigType',
    label: '产品大类',
    formatter: row => PRODUCT_CATEGORY[row.bigType]
  }, {
    width: '100',
    prop: 'stockNum',
    label: '库存数量'
  }
  // {
  //   minWidth: '200',
  //   prop: 'platformCategory',
  //   label: '产品类目',
  //   'show-overflow-tooltip': true,
  //   formatter: row => {
  //     let categoryList = []
  //       .concat(row.platformPrimaryCategoryName || [])
  //       .concat(row.platformSecondCategoryName || [])
  //       .concat(row.platformThirdCategoryName || [])
  //     return categoryList.join('-')
  //   }
  // }
]

export const commonProductListColumns = [
  { type: 'selection', width: '50' },
  { minWidth: '150', prop: 'platCode', label: '平台编码' },
  { minWidth: '150', prop: 'nameCn', label: '产品名称' },
  { minWidth: '150', prop: 'specification', label: '规格型号', 'show-overflow-tooltip': true },
  { minWidth: '150', prop: 'manufacturer', label: '生产厂家' },
  { minWidth: '180', prop: 'registrationNumber', label: '注册证号（或备案号）' },
  { width: '120',
    prop: 'price',
    label: '单价',
    formatter: row => formatPrice(row.price)
  },
  { width: '180', prop: 'purchaseNum', label: '采购数量' },
  { width: '100', prop: 'unit', label: '计量单位' },
  { minWidth: '120',
    prop: 'manageType',
    label: '管理分类',
    formatter: row => PRODUCT_MANAGE_CATEGORY[row.manageType]
  },
  { width: '120',
    prop: 'registerArea',
    label: '注册地',
    formatter: row => PRODUCT_REGISTER_TYPE[row.registerArea]
  },
  {
    width: '120',
    prop: 'productCategory',
    label: '产品大类',
    formatter: row => PRODUCT_CATEGORY[row.productCategory]
  },
  { minWidth: '200',
    prop: 'platformCategory',
    label: '产品类目',
    'show-overflow-tooltip': true,
    formatter: row => {
      let categoryList = [].concat(row.platformPrimaryCategoryName || []).concat(row.platformSecondCategoryName || []).concat(row.platformThirdCategoryName || [])
      return categoryList.join('-')
    }
  }
]
export const productsPriceColumns = [
  { type: 'selection', width: '50' },
  { minWidth: '150', prop: 'platCode', label: '产品编码' },
  { minWidth: '150', prop: 'categoryInfoList', label: '产品分类' },
  { minWidth: '150', prop: 'nameCn', label: '产品名称' },
  { minWidth: '150', prop: 'specification', label: '规格', 'show-overflow-tooltip': true },
  { minWidth: '150', prop: 'specification', label: '型号', 'show-overflow-tooltip': true },
  { width: '100', prop: 'unit', label: '最小单位' },
  { minWidth: '150', prop: 'manufacturer', label: '生产厂家' },
  { minWidth: '180', prop: 'registrationNumber', label: '注册证号（或备案号）' },
  { minWidth: '120',
    prop: 'manageType',
    label: '管理分类',
    formatter: row => PRODUCT_MANAGE_CATEGORY[row.manageType]
  },
  {
    width: '120',
    prop: 'productCategory',
    label: '产品大类',
    formatter: row => PRODUCT_CATEGORY[row.productCategory]
  }
]

export const commonProductListSearchForm = [
  {
    key: 'idList',
    title: '产品名称',
    type: 'select',
    attr: {
      placeholder: '请选择',
      filterable: true,
      remote: true
    }
  },
  {
    key: 'platCode',
    title: '平台编码',
    type: 'select',
    attr: {
      placeholder: '请选择',
      filterable: true,
      remote: true
    }
  },
  {
    key: 'bigType',
    title: '产品大类',
    type: 'select',
    attr: {
      placeholder: '请选择'
    },
    options: Object.keys(PRODUCT_CATEGORY).map(key => {
      return {
        label: PRODUCT_CATEGORY[key],
        value: key
      }
    })
  },
  {
    key: 'manufacturer',
    title: '生产厂家',
    type: 'input',
    attr: {
      placeholder: '请输入'
    }
  }
]
export const productsPriceSearchForm = [
  {
    key: 'barCode',
    title: '产品编码',
    type: 'input',
    attr: {
      placeholder: '请输入'
    }
  },
  {
    key: 'idList',
    title: '产品名称',
    type: 'select',
    attr: {
      placeholder: '请选择',
      filterable: true,
      remote: true
    }
  },
  {
    key: 'manageType',
    title: '管理分类',
    type: 'select',
    attr: {
      placeholder: '请选择'
    },
    options: Object.keys(PRODUCT_MANAGE_CATEGORY).map(key => {
      return {
        label: PRODUCT_MANAGE_CATEGORY[key],
        value: key
      }
    })
  },
  {
    key: 'bigType',
    title: '产品大类',
    type: 'select',
    attr: {
      placeholder: '请选择'
    },
    options: Object.keys(PRODUCT_CATEGORY).map(key => {
      return {
        label: PRODUCT_CATEGORY[key],
        value: key
      }
    })
  },
  {
    key: 'category',
    title: '产品分类',
    type: 'cascader',
    options: [],
    optionProps: {
      checkStrictly: true
    },
    attr: {
      placeholder: '请选择'
    }
  },
  {
    key: 'manufacturer',
    title: '生产厂家',
    type: 'input',
    attr: {
      placeholder: '请输入'
    }
  }
]
export const SearchFormElse = [
  {
    key: 'prodNameCn',
    title: '产品名称',
    type: 'input',
    attr: {
      placeholder: '请输入',
      clearable: true
    }
  },
  {
    key: 'platCode',
    title: '平台编码',
    type: 'input',
    attr: {
      placeholder: '请输入'
    }
  },
  {
    key: 'bigType',
    title: '产品大类',
    type: 'select',
    attr: {
      clearable: true,
      placeholder: '请选择'
    },
    options: Object.keys(PRODUCT_CATEGORY).map(key => {
      return {
        label: PRODUCT_CATEGORY[key],
        value: key
      }
    })
  },
  {
    key: 'manufacturer',
    title: '生产厂家',
    type: 'input',
    attr: {
      clearable: true,
      placeholder: '请输入'
    }
  },
  {
    key: 'companyProdCode',
    title: '产品编码',
    type: 'input',
    attr: {
      placeholder: '请输入'
    }
  },
  {
    key: 'registNo',
    title: '注册证号',
    type: 'input',
    attr: {
      clearable: true,
      placeholder: '请输入'
    }
  }
]

export function isNull (o) {
  return o === null || o === void 0
}
// todo 添加全局方法，重写此方法
// 格式化价格方法 100 => ￥100.00 100.356 => ￥100.35
export function formatPrice (val, cancelFixed) {
  // if (!price) return '￥0.000'
  // const priceNum = Number(price)
  // if (priceNum % 1 === 0) {
  //   return '￥' + priceNum + '.000'
  // }
  // if ((priceNum * 10) % 1 === 0) {
  //   return '￥' + priceNum + '0'
  // }
  // return '￥' + Math.round(priceNum * 100) / 100
  if (isNull(val) || isNaN(val)) return
  let USPrice = Number.parseFloat(val).toLocaleString()
  let lastDot = USPrice.toString().indexOf('.')
  // 完全是整数, 需要添加小数点
  if (lastDot === -1 && !cancelFixed) {
    USPrice += '.000'
  }
  // 返回数据是一位小数，用0补齐为两位小数
  if (USPrice.toString().substring(lastDot + 1).length === 1 && !cancelFixed) {
    USPrice += '00'
  }
  if (USPrice.toString().substring(lastDot + 1).length === 2 && !cancelFixed) {
    USPrice += '0'
  }
  if (USPrice.toString().substring(lastDot + 1).length === 3 && !cancelFixed) {
    USPrice += ''
  }
  return USPrice
}
// 初始化注册证基本信息
export function regisNoButton (data, str = 'registration') {
  let number = `${str}Number`
  let id = `${str}Id`
  if (str instanceof Array) {
    number = str[0] || ''
    id = str[1] || ''
  }
  if (data[number]) {
    return data[number]
  } else if (!data[number] && data[id]) {
    return '暂无注册证号'
  } else if (!data[number] && !data[id]) {
    return '暂无数据'
  }
}
// 查看注册证
export function showPdfRegistion (id) {
  this.$axios({
    method: 'get',
    url: getCertificate,
    params: {
      id: id
    }
  }).then(res => {
    let obj = hasIn(res, 'data.payload') ? (res.data.payload || {}) : {}
    let imageList = obj.imageList || []
    if (imageList.length < 1) {
      this.$message({
        type: 'warning',
        message: '获取失败，请在产品管理中维护该产品的注册证信息'
      })
    } else {
      this.$pdfViewer({ url: imageList[0].imageUrl, title: '注册证预览' })
    }
  })
  .catch((err) => { console.log(err) })
}
